package com.clwl.supervise.superviseapp.service.base;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clwl.supervise.superviseapp.entity.base.SysArea;
import com.clwl.supervise.superviseapp.entity.base.SysDictData;
import com.clwl.supervise.superviseapp.entity.base.TsDept;
import com.clwl.supervise.superviseapp.entity.base.TsUser;
import com.clwl.supervise.superviseapp.mapper.base.SysAreaMapper;
import com.clwl.supervise.superviseapp.mapper.base.SysDictDataMapper;
import com.clwl.supervise.superviseapp.mapper.base.TsDeptMapper;
import com.clwl.supervise.superviseapp.mapper.base.TsUserMapper;
import com.clwl.supervise.superviseapp.util.UserHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService extends ServiceImpl<TsUserMapper, TsUser> {

    @Resource
    private TsUserMapper userMapper;

    @Resource
    private TsDeptMapper deptMapper;

    @Resource
    private SysAreaMapper sysAreaMapper;
    @Resource
    private SysDictDataMapper dictDataMapper;
    private final String POSITION_CODE = "position_code";

    public TsUser selectUserInfo(Long userId) {
        TsUser user = userMapper.selectById(userId);
        if (user.getDeptId() != null) {
            TsDept dept = deptMapper.selectById(user.getDeptId());
            user.setDeptName(dept == null ? "" : dept.getName());
        }
        if (user.getAreaId() != null) {
            SysArea area = sysAreaMapper.selectById(user.getAreaId());
            user.setAreaName(area == null ? "" : area.getName());
        }
        return user;
    }

    public TsUser selectByUserName(String username) {
        return userMapper.selectByUserName(username);
    }

    /**
     * 获取我的功能基础信息包含账户信息
     *
     * @return
     */
    public Map<String, Object> getMyUserInfo() {
        Map<String, Object> resultMap = new HashMap<>();
        Long userId = UserHelper.getUserId();
        TsUser user = userMapper.selectById(userId);
        if (null == user) {
            return null;
        }
        resultMap.put("userName", user.getName());
        resultMap.put("mobile", StringUtils.isBlank(user.getMobile()) ? user.getTel() : user.getMobile());
        resultMap.put("picUrl", StringUtils.isBlank(user.getPicUrl()) ? "" : user.getPicUrl());

        //如果部门ID为空 设置默认部门名称
        if (user.getDeptId() == null) {
            resultMap.put("deptName", "");
        } else {
            TsDept dept = deptMapper.selectById(user.getDeptId());
            user.setDeptName(dept == null ? "" : dept.getName());
            resultMap.put("deptName", dept == null ? "" : dept.getName());
            TsDept tsDept = deptMapper.selectById(dept.getParentId());
            resultMap.put("areaName", tsDept !=null?tsDept.getNickname():"");
        }
        if (StringUtils.isNotEmpty(user.getPositionCode())) {
            SysDictData sysDictData = dictDataMapper.queryDictInfoByTypeCode(POSITION_CODE, user.getPositionCode());
            resultMap.put("positionName", sysDictData != null ? sysDictData.getDictLabel() : "");
        } else {
            resultMap.put("positionName", "");
        }

        //如果区域ID为空，设置默认区域名称
//        if (user.getAreaId() == null) {
//            resultMap.put("areaName", "");
//        }else{
//            SysArea area = sysAreaMapper.selectById(user.getAreaId());
//            user.setAreaName(area == null ? "" : area.getName());
//            resultMap.put("areaName", area == null ? "" : area.getName());
//        }


        return resultMap;
    }
}
